package com.ssyt.hiring.talent.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.App;
import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.talent.entity.ResumeEntity;
import com.ssyt.hiring.talent.service.ResumeService;

public class ResumeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ResumeService resumeService = new ResumeService();

	public ResumeServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("toResumeAddUI".equals(command)) {
			toResumeAddUI(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		}
	}

	/**
	 * 转到添加简历的页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toResumeAddUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/talent/resume-add.jsp")
				.forward(request, response);
	}

	/**
	 * 添加简历
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");

		int talentId = App.getTalentId(request, response);
		ResumeEntity resume = new ResumeEntity();
		resume.setIntention(intention);
		resume.setWorkExperience(workExperience);
		resume.setProjectExperience(projectExperience);
		resume.setTalentId(talentId);
		int row = resumeService.add(resume);

		if (row == 0) {
			request.setAttribute("resume", resume);
			request.getRequestDispatcher("/WEB-INF/view/talent/resume-add.jsp")
					.forward(request, response);
		} else {
			// 重定向，地址变化，不能通过request传递参数
			response.sendRedirect("TalentServlet?command=main");
			// 转发，地址不变（属于一次服务跳转），可以通过request传递参数
			// List<ResumeEntity> resumeList = resumeService
			// .listResumeByTalentId(talentId);
			// request.setAttribute("resumeList", resumeList);
			// request.getRequestDispatcher("/WEB-INF/view/talent/main.jsp")
			// .forward(request, response);
		}
	}

	/**
	 * 删除指定的简历
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));

		int row = resumeService.delete(resumeId);

		if (row == 0) {
			// 给页面打印一个fail文本
			response.getWriter().print("fail");
		}
	}
}
